package com.lhkj.ct.base.mybatis.extension.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.lhkj.ct.base.mybatis.extension.service.IService;
import com.lhkj.ct.base.spring.SpringContextHolder;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 基础接口
 *
 * @Date: 2023-08-22
 */
@SuppressWarnings("unchecked")
public class IServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {

    protected Log log = LogFactory.getLog(this.getClass());

    protected Class<T> entityClass = this.currentModelClass();

    protected Class<M> mapperClass = this.currentMapperClass();

    @Autowired
    protected M baseMapper;

    public IServiceImpl() {
    }

    public M getBaseMapper() {
        if (null == baseMapper) {
            baseMapper = SpringContextHolder.getBean(this.currentMapperClass());
        }
        return baseMapper;
    }

    public Class<T> getEntityClass() {
        return this.entityClass;
    }

    protected Class<M> currentMapperClass() {
        return (Class<M>)ReflectionKit.getSuperClassGenericType(this.getClass(), IServiceImpl.class, 0);
    }

    protected Class<T> currentModelClass() {
        return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), IServiceImpl.class, 1);
    }

}
